<?php

/*
** COMITATO PROVINCIALE DI CATANIA - CROCE ROSSA ITALIANA
*/

class Database {
	public
		$numeroQuery;
	private
		$meta;
	public function __construct() {
		global $conf;
		$this->meta = mysql_connect(
			$conf['db']['host'],
			$conf['db']['user'],
			$conf['db']['pass']);
		mysql_select_db(
			$conf['db']['daba'],
			$this->meta
		);
		$this->numeroQuery = 0;
	}

	public function query ($q) {
		$this->numeroQuery++;
		return mysql_query(
			$q,
			$this->meta
			);
	}

	public function escape ($stringa) {
		return mysql_real_escape_string(
			$stringa,
			$this->meta
			);
	}

	public function fetch($r) {
		return
			mysql_fetch_array(
				$r,
				MYSQL_NUM
			);
	}

	// Alias per Fetch
	public function fetch_array( $r ) {
		return $this->fetch($r);
	}

	public function riga ($q) {
		$q = $this->query($q);
		if (!$q) { return false; }
		$q = $this->fetch_array($q);
		return $q;
	}

	public function singolo ($q) {
		$q = $this->riga($q);
		if (!$q) { return false; }
		return $q[0];
	}

	public function conta ($q2) {
		$q = 'SELECT COUNT(*) FROM ' .
			$q2;
		$q = $this->singolo($q);
		return (int) $q;
	}

	// Chiamato alla chiusura del programma
	public function __destruct() {
		return mysql_close(
			$this->meta
		);
	}
}
